/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package xml;

import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.Element;
import javax.xml.parsers.DocumentBuilder;
import no.met.metdata.proxy.*;
import org.w3c.dom.NodeList;

/**
 *
 * @author 490060
 */
public class WeatherFinder 
{
    private final float KELVIN = 273.15f;
    
    private ArrayList<Float> temperatures = null;
    
    
    public ArrayList<Float> getTemperatures()
    {
        return temperatures;
    }
    public WeatherFinder()
    {
        
    }
    
    public no_met_metdata_StationProperties getStation(String county, String name)
    {
        ArrayList<no_met_metdata_StationProperties> list = new ArrayList();
        MetServiceProxy mt = new MetServiceProxy();
        no_met_metdata_StationProperties[] station = null;
        try 
        {
            //station = mt.getStationsProperties(null, null);
            station = mt.getStationsFromTimeserieType("2", "");
 
            for(int i = 0; i < station.length; i++)
            {
                //System.out.println("Station: " + station[i].getDepartment() + "\t" + station[i].getName());
                if(county.toUpperCase().equals(station[i].getDepartment()) 
                        && name.toUpperCase().equals(station[i].getName()))
                {
                    list.add(station[i]);
                    System.out.println(station[i].getStnr());
                }
            }
        } catch (Exception ex) 
        {
            Logger.getLogger(WeatherFinder.class.getName()).log(Level.SEVERE, null, ex);
        }
        return list.get(0);
    }
    
    public ArrayList<Float> getTemperatures(String start, String end, String county, String stationName, String hours)
    {//18700,50540". "0,6,12,18""2006-01-01"
        ArrayList<Float> temperatures = new ArrayList<Float>();
        try {
            no_met_metdata_StationProperties station = getStation(county, stationName);
            String stationCode = station.getStnr() + "";
            String Startdate = station.getFromYear() + "-" 
                        + station.getFromMonth() + "-"
                        + station.getFromDay();
            String EndDate = station.getToYear() + "-"
                    + station.getToMonth() + "-"
                    + station.getToDay();
            System.out.println("STARTDATE: " + Startdate + "\nEND: " + EndDate);
            System.out.println(stationCode);
            MetServiceProxy mt = new MetServiceProxy();
            org.w3c.dom.Element data = mt.getMetDataValues("2", "", start, 
            end, stationCode, "tax,tan", hours , "", "");
            NodeList weatherElements = data.getElementsByTagName("weatherElement");
            for(int i = 0; i < weatherElements.getLength(); i += 2)
            {
                float min = Float.parseFloat(weatherElements.item(i).getTextContent()) + 273.15f;
                float max = Float.parseFloat(weatherElements.item(i + 1).getTextContent()) + 273.15f;
                float temperature = (min + max) / 2 - KELVIN;
                temperatures.add(temperature);
            }
        } catch (Exception ex) {
            Logger.getLogger(WeatherFinder.class.getName()).log(Level.SEVERE, null, ex);
        }
        this.temperatures = temperatures;
        return temperatures;
    }
    
    public static void main(String[] args)
    {
        try 
        {
            WeatherFinder wf = new WeatherFinder();
            ArrayList test = wf.getTemperatures("2005-09-01", "2005-09-02", "TROMS", "TROMSØ", "0,6,12,18");
            for(int i = 0; i < test.size(); i++)
                System.out.println("Temp: " + test.get(i));
            
            
        } catch (Exception ex) 
        {
            Logger.getLogger(WeatherFinder.class.getName()).log(Level.SEVERE, null, ex);
            ex.printStackTrace();
        }
    }
}
